﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Microsoft.ProductivityHub.SilverlightBookshelf
{
    public partial class Bookshelf : UserControl
    {
        private int currentTabIndex = 1;
		private int selectedTabIndex = 1;
        private Border lastCollapsedBorder = null;
        private const int _collapsedWidth = 36;
        private const int _expandedWidth = 526;

        public delegate void FavoritesUpdated(UserControl source);


		Storyboard[] hoverAnimations;
		Storyboard[] collapseAnimations;

        public Bookshelf()
        {
            InitializeComponent();
            
            Init();
            
            // wire up to handle the event for Favorites being updated 
            FeaturedVideoControl1.OnFavoritesUpdated += new FavoritesUpdated(Bookshelf_OnFavoritesUpdated);
            FeaturedContentControl1.OnFavoritesUpdated += new FavoritesUpdated(Bookshelf_OnFavoritesUpdated);
            TopRatedControl1.OnFavoritesUpdated += new FavoritesUpdated(Bookshelf_OnFavoritesUpdated);
            
            hoverAnimations = new Storyboard[4];
            hoverAnimations[0] = CreateHoverStoryboard(Tab1, TabTitle1);
            hoverAnimations[1] = CreateHoverStoryboard(Tab2, TabTitle2);
            hoverAnimations[2] = CreateHoverStoryboard(Tab3, TabTitle3);
            hoverAnimations[3] = CreateHoverStoryboard(Tab4, TabTitle4);

            collapseAnimations = new Storyboard[4];
            collapseAnimations[0] = CreateCollapseStoryboard(Tab1, TabTitle1);
            collapseAnimations[1] = CreateCollapseStoryboard(Tab2, TabTitle2);
            collapseAnimations[2] = CreateCollapseStoryboard(Tab3, TabTitle3);
            collapseAnimations[3] = CreateCollapseStoryboard(Tab4, TabTitle4);
        }

       


        private void Init()
        {
            Tab1.Width = _expandedWidth;
            Tab2.Width = _collapsedWidth;
            Tab3.Width = _collapsedWidth;
            Tab4.Width = _collapsedWidth;

            // ensure the favorites control is loaded 
            FavoritesControl1.LoadData();

            // show first tab child controls
            ((Grid)Tab1.Child).Visibility = System.Windows.Visibility.Visible;

            
        }

        private Storyboard CreateCollapseStoryboard(DependencyObject tabTarget, DependencyObject verticalTabTarget)
        {
            Storyboard sb = new Storyboard();
            // Double animation definition for the Width animation
            DoubleAnimation widthAnimation = new DoubleAnimation();
            Duration duration = new Duration(TimeSpan.FromMilliseconds(350));
            widthAnimation.Duration = duration;
            widthAnimation.From = _expandedWidth;
            widthAnimation.To = _collapsedWidth;
            sb.Duration = duration;
            sb.Children.Add(widthAnimation);

            Storyboard.SetTarget(widthAnimation, tabTarget);
            Storyboard.SetTargetProperty(widthAnimation, new PropertyPath("Width"));

            // Double animation definition for the Color animation
            /*
            ColorAnimation colorAnimation = new ColorAnimation();
            Duration coDuration = new Duration(TimeSpan.FromMilliseconds(350));
            colorAnimation.Duration = coDuration;
            colorAnimation.From = BindableValues.HubButtonFace;
            colorAnimation.To = Colors.Transparent;
            sb.Duration = coDuration;
            sb.Children.Add(colorAnimation);
            
            Storyboard.SetTarget(colorAnimation, gridTarget);
            Storyboard.SetTargetProperty(colorAnimation, new PropertyPath("Background"));
            */


            // Double animation definition for the Opacity animation
            
            DoubleAnimation opacityAnimation = new DoubleAnimation();
            Duration opDuration = new Duration(TimeSpan.FromMilliseconds(450)); // on collapse, delay the showing of the title
            opacityAnimation.Duration = opDuration;
            opacityAnimation.From = 0;
            opacityAnimation.To = 1;
            sb.Duration = opDuration;
            sb.Children.Add(opacityAnimation);

            Storyboard.SetTarget(opacityAnimation, verticalTabTarget);
            Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));
            

            // sb.Completed += new EventHandler(SB_Collapse_Completed);
            return sb;

        }

        void SB_Collapse_Completed(object sender, EventArgs e)
        {
            // wait for the collapse animation to complete before changing its background to the gradient brush
            if (lastCollapsedBorder != null)
            {
                Grid oldTab = (Grid)lastCollapsedBorder.Child;
                oldTab.Background = BindableValues.ButtonGradientBrush;
            }
            
        }

        private Storyboard CreateHoverStoryboard(DependencyObject tabTarget, DependencyObject verticalTabTarget)
        {
            // Double animation definition for the Width animation
            Storyboard sb = new Storyboard();
            DoubleAnimation widthAnimation = new DoubleAnimation();
            Duration duration = new Duration(TimeSpan.FromMilliseconds(350));
            widthAnimation.Duration = duration;
            widthAnimation.From = _collapsedWidth;
            widthAnimation.To = _expandedWidth;
            sb.Duration = duration;
            sb.Children.Add(widthAnimation);

            Storyboard.SetTarget(widthAnimation, tabTarget);
            Storyboard.SetTargetProperty(widthAnimation, new PropertyPath("Width"));

            /*
           // Double animation definition for the Color animation
           
           ColorAnimation colorAnimation = new ColorAnimation();
           Duration coDuration = new Duration(TimeSpan.FromMilliseconds(350));
           colorAnimation.Duration = coDuration;
           colorAnimation.From = Colors.Transparent;
           colorAnimation.To = BindableValues.HubButtonFace;
           sb.Duration = coDuration;
           sb.Children.Add(colorAnimation);

           Storyboard.SetTarget(colorAnimation, gridTarget);
           Storyboard.SetTargetProperty(colorAnimation, new PropertyPath("Background"));
           */

            // Double animation definition for the Opacity animation
           /*
           DoubleAnimation opacityAnimation = new DoubleAnimation();
           Duration opDuration = new Duration(TimeSpan.FromMilliseconds(650)); // on show, speed up the hiding of the title
           opacityAnimation.Duration = opDuration;
           opacityAnimation.From = 1;
           opacityAnimation.To = 0;
           sb.Duration = opDuration;
           sb.Children.Add(opacityAnimation);

           Storyboard.SetTarget(opacityAnimation, verticalTabTarget);
           Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));
           */
            return sb;

        }

        /// <summary>
        /// Handles the event from the 3 child controls - when favorites are updated, force a refresh of the data for the favorites control
        /// </summary>
        /// <param name="source"></param>
        void Bookshelf_OnFavoritesUpdated(UserControl source)
        {
            FavoritesControl1.LoadData();

        }

        private void AnimateTab(Border selectedTab, int currentTabIndex, int selectedTabIndex)
        {
            if (FeaturedVideoControl1 != null)
            {   
                // issue pause video command if the tabs are changing
                FeaturedVideoControl1.PauseVideo();
            }

            // Kick off animations
            collapseAnimations[currentTabIndex - 1].Begin();
            hoverAnimations[selectedTabIndex - 1].Begin();

            Grid oldTab = (Grid)((Border)BookshelfPanel.Children[currentTabIndex - 1]).Child;
            Grid oldContentGrid = (Grid)oldTab.Children[1];
            
            Grid newTab = (Grid)selectedTab.Child;  // Grid1
            Grid newContentGrid = (Grid)newTab.Children[1];

            // Perform any data updates as needed
            /*
            if (newContentGrid.Children.Count > 0 && newContentGrid.Children[0] is MyFavoritesControl)
            {
                // call the LoadData function
                MyFavoritesControl favControl = newContentGrid.Children[0] as MyFavoritesControl;
                favControl.LoadData();
            }
            */
            

            // Change state of tab -- hide outgoing tab's Content area, but show its title area
            oldTab.Background = BindableValues.ButtonGradientBrush;
            // VerticalTab oldTitle = (VerticalTab)oldTab.Children[0]; // let the animation hanlde this 
            // oldTitle.Visibility = System.Windows.Visibility.Visible; // let the animation handle this
            oldContentGrid.Visibility = System.Windows.Visibility.Collapsed;

            // Change state of tab -- show the new tab child controls
            newTab.Background = BindableValues.ContentGradientBrush;
            VerticalTab newTitle = (VerticalTab)newTab.Children[0]; // let the animation handle this
            newTitle.Opacity = 0; // System.Windows.Visibility.Collapsed; // TODO: let the animation handle this
            newContentGrid.Visibility = System.Windows.Visibility.Visible;
        }

        private void Tab1_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            selectedTabIndex = 1;
            if (currentTabIndex != selectedTabIndex)
            {
                AnimateTab((Border)sender, currentTabIndex, selectedTabIndex);
            }
            currentTabIndex = 1;
        }

        private void Tab2_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            selectedTabIndex = 2;
            if (currentTabIndex != selectedTabIndex)
            {
                AnimateTab((Border)sender, currentTabIndex, selectedTabIndex);
            }
            currentTabIndex = 2;

        }

        private void Tab3_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            selectedTabIndex = 3;
            if (currentTabIndex != selectedTabIndex)
            {
                AnimateTab((Border)sender, currentTabIndex, selectedTabIndex);
            }
            currentTabIndex = 3;
        }

        private void Tab4_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            selectedTabIndex = 4;
            if (currentTabIndex != selectedTabIndex)
            {
                AnimateTab((Border)sender, currentTabIndex, selectedTabIndex);
            }
            currentTabIndex = 4;
        }
		
    }
}
